package com.example.demodb.controller;

import lombok.Cleanup;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author sin
 * @date 2022/11/25
 * @apiNote
 */
@Slf4j
@RestController
public class TestController {

    @Autowired
    DataSource dataSource;

    @RequestMapping("/test")
    public String testControler() throws Exception {
        System.out.println("测试成功");
        @Cleanup Connection conn = dataSource.getConnection();
        @Cleanup Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        stmt.setFetchSize(Integer.MIN_VALUE);
        long start = System.currentTimeMillis();
        @Cleanup ResultSet rs = stmt.executeQuery("SELECT * FROM corr_news_backup");
        loopResultSet(rs);
        log.info("   流式查询耗时 :: {} ", (System.currentTimeMillis() - start) / 1000);
        return "测试成功";
    }



    @SneakyThrows
    private Long loopResultSet(ResultSet rs) {
        int i =0;
        while (rs.next()) {
            // 业务操作
//            String book_name = rs.getString("book_name");
//            log.info(book_name);
            i++;
            if(i%10000==0){
                log.info(i+"");
            }
        }
        return 1L;
    }
}
